package com.it.App;

import com.it.util.HtmlParseUtil;
import com.it.mapper.bookMapper;
import com.it.pojo.BookBreed;
import com.it.pojo.book;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class parsebook {
    public static void main(String[] args) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //开启事物
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        bookMapper mapper = sqlSession.getMapper(bookMapper.class);
        //keywords是名称 page是爬取的页数(规律是1,3,5,7,9,11,13.......),京东页面展示总共100页,page值为199
        for (int i = 1; i <= 99;i+=2) {
            List<book> bookList = HtmlParseUtil.HTmlJdUtil("情感书籍",i);
            for (book book : bookList) {
                //添加数据到book数据表中
                mapper.addBook(book);
            }
        }
        //查询出类别数量
        List<BookBreed> bookBreeds = mapper.selectBookNumber();
        System.out.println(bookBreeds);
        //在添加前先进行数据的清空
        mapper.dropAll();
        for (BookBreed bookBreed : bookBreeds) {
            //存入在bookBreed表中
            mapper.addBreed(bookBreed);
        }
        sqlSession.close();
    }

}
